LEFT JOIN

In this lesson, we will discuss the LEFT JOIN keyword.

We'll cover the following

LEFT JOIN#

The LEFT JOIN keyword returns all records from the left table (table1), and the matched records from the right table (table2). The result is NULL from the right side if there is no match.

Syntax#

SELECT table1.column1, table2.column2...

FROM table1

LEFT JOIN table2

ON table1.common_field = table2.common_field;

Note: In some databases, LEFT JOIN is called LEFT OUTER JOIN.

Example#

We want to select all customers and any orders they might have placed:

Created with Fabric.js 1.6.0-rc.1 The CUSTOMERS table contains information regarding the customers, while the ORDERS tablecontains information regarding orders placed by customers. As we wantthe customer infromation even if they have not placed an order, so we will use LEFT JOIN.
1 of 3

The SQL query to retrieve all customers whether or not they have placed an order:

As you can see, the LEFT JOIN keyword returns all records from the left table (Customers), even if there are no matches in the right table (Orders).

Quick quiz!#

Q

Will the following query return the customer NAME and ADDRESS along with the AMOUNT they purchased?

SELECT CUSTOMERS.NAME, CUSTOMERS.ADDRESS , ORDERS.AMOUNT
FROM CUSTOMERS
LEFT JOIN ORDERS
ON CUSTOMERS.ID = ORDERS.CUSTOMER_ID;
A)

True

B)

False


In the next lesson, we will discuss the RIGHT JOIN keyword.

INNER JOIN
RIGHT JOIN
Mark as Completed
Report an Issue